package com.hanhanxiaochong.zhongxiangchuanmei.view.shop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShoppingCartItemBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.DensityUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.ShoppingcartAmountView;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.checkbox.SmoothCheckBox;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by RockPhoenix on 2018/5/26.
 */

public class ShoppingCartAdapter extends CommonAdapter<ShoppingCartItemBean> {

    private OnGoodSelectListener onGoodSelectListener;
    private OnMoneyChangedListener onMoneyChangedListener;
    private List<ShoppingCartItemBean.ListBean> selectList;
    //选中商品得总价
    private double selectPrice;
    //选中条目的索引与shopping_id集合,K=条目位置,V=shopping_id
//    private Map<Integer, String> shoppingIdMap;

    public ShoppingCartAdapter(Context context, int layoutId, List<ShoppingCartItemBean> datas) {
        super(context, layoutId, datas);
        selectList = new ArrayList<>();
//        shoppingIdMap = new HashMap<>();
    }

    @Override
    protected void convert(ViewHolder holder, ShoppingCartItemBean bean, int position) {
        for (ShoppingCartItemBean.ListBean listBean : bean.getList()) {
            if (listBean.isSelected()) {
                selectPrice += Double.parseDouble(listBean.getPrice()) * Double.parseDouble(listBean.getShop_num());
            }
        }

        LinearLayout llShopTitle = holder.getView(R.id.ll_shop_title);
        View viewDivide = holder.getView(R.id.view_divide);
        SmoothCheckBox cbShop = holder.getView(R.id.cb_shop);
        TextView shopName = holder.getView(R.id.tv_shop_name);
        LinearLayout llContainer = holder.getView(R.id.ll_good_container);
        ViewGroup.LayoutParams layoutParams1 = cbShop.getLayoutParams();
        layoutParams1.width = DensityUtil.dip2px(20, mContext);
        layoutParams1.height = DensityUtil.dip2px(20, mContext);
        cbShop.requestLayout();
        cbShop.setChecked(bean.isSelected(), false);
        shopName.setText(bean.getC_name());
        if (position > 0 && mDatas.get(position - 1).getC_name().equals(mDatas.get(position).getC_name())) {
            llShopTitle.setVisibility(View.GONE);
            viewDivide.setVisibility(View.GONE);
        } else {
            llShopTitle.setVisibility(View.VISIBLE);
            viewDivide.setVisibility(View.VISIBLE);
        }

        cbShop.setOnToggleClickListener(isChecked -> {
            bean.setSelected(cbShop.isChecked());
            setBeanSelectedStatus(bean, isChecked);
            for (ShoppingCartItemBean.ListBean listBean : bean.getList()) {
                if (isChecked) {
                    if (!selectList.contains(listBean))
                        selectList.add(listBean);
                } else {
                    if (selectList.contains(listBean))
                        selectList.remove(listBean);
                }
            }
            onGoodSelectListener.onGoodSelect();
        });

        llShopTitle.setOnClickListener(v -> {
            cbShop.setChecked(!cbShop.isChecked());
            bean.setSelected(cbShop.isChecked());
            setBeanSelectedStatus(bean, cbShop.isChecked());
        });

        llContainer.removeAllViews();
        for (ShoppingCartItemBean.ListBean listBean : bean.getList()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_shopping_cart_good, llContainer, false);
            GoodViewHolder viewHolder = new GoodViewHolder(view);
            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + listBean.getPic().split(",")[0],
                    R.mipmap.img_error, R.mipmap.img_error, viewHolder.ivProjectPic);
            ViewGroup.LayoutParams layoutParams2 = viewHolder.cbGood.getLayoutParams();
            layoutParams2.width = DensityUtil.dip2px(20, mContext);
            layoutParams2.height = DensityUtil.dip2px(20, mContext);
            viewHolder.cbGood.requestLayout();
            viewHolder.cbGood.setChecked(listBean.isSelected(), false);
            viewHolder.cbGood.setOnToggleClickListener(isChecked -> {
                if (isChecked) {
                    if (!selectList.contains(listBean))
                        selectList.add(listBean);
                } else {
                    if (selectList.contains(listBean))
                        selectList.remove(listBean);
                }
                onGoodSelectListener.onGoodSelect();
            });
            viewHolder.ivSelfSupport.setVisibility(listBean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
            viewHolder.tvGoodName.setText(listBean.getB_name());
            viewHolder.tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), listBean.getPrice_vip()));
            viewHolder.tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), listBean.getPrice()));
            viewHolder.countView.setOnBtnClickListener(new ShoppingcartAmountView.BtnClickListener() {
                @Override
                public void onBtnClickPlus(View view, int amount) {
                    viewHolder.countView.setNum(++amount);
                    listBean.setShop_num(String.valueOf(amount));
                    if (selectList.contains(listBean)) {
                        selectList.set(selectList.indexOf(listBean), listBean);
                        onMoneyChangedListener.onMoneyChanged();
                    }
                }

                @Override
                public void onBtnClickDecrease(View view, int amount) {
                    viewHolder.countView.setNum(--amount);
                }
            });
            llContainer.addView(view);
            viewHolder.itemView.setOnClickListener(v -> {
                viewHolder.cbGood.setChecked(!viewHolder.cbGood.isChecked());
                if (viewHolder.cbGood.isChecked()) {
                    if (!selectList.contains(listBean))
                        selectList.add(listBean);
                } else {
                    if (selectList.contains(listBean))
                        selectList.remove(listBean);
                }
                onGoodSelectListener.onGoodSelect();
            });
        }
    }

    private void setBeanSelectedStatus(ShoppingCartItemBean bean, boolean isChecked) {
        for (ShoppingCartItemBean.ListBean itemBean :
                bean.getList()) {
            itemBean.setSelected(isChecked);
        }
        notifyDataSetChanged();
    }

    public List<ShoppingCartItemBean.ListBean> getSelectList() {
        return selectList;
    }

    class GoodViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cb_good)
        SmoothCheckBox cbGood;
        @BindView(R.id.iv_project_pic)
        ImageView ivProjectPic;
        @BindView(R.id.iv_self_support)
        ImageView ivSelfSupport;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_vip_price)
        TextView tvVipPrice;
        @BindView(R.id.tv_normal_price)
        TextView tvNormalPrice;
        @BindView(R.id.count_view)
        ShoppingcartAmountView countView;

        GoodViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnGoodSelectListener {

        void onGoodSelect();

    }

    public void setOnGoodSelectListener(OnGoodSelectListener onGoodSelectListener) {
        this.onGoodSelectListener = onGoodSelectListener;
    }

    public interface OnMoneyChangedListener {
        void onMoneyChanged();
    }

    public void setOnMoneyChangedListener(OnMoneyChangedListener onMoneyChangedListener) {
        this.onMoneyChangedListener = onMoneyChangedListener;
    }
}
